/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *
 */

/*
 * CIIFConfigure.java
 *
 * Created on Jun 6, 2009, 2:13:56 AM
 */

package ciif;

import java.io.File;
import java.util.prefs.*;
import java.awt.*;


/**
 *
 * @author drano
 */
public class CIIFConfigure extends javax.swing.JFrame {

    private javax.swing.JFileChooser fileChooser;
    private Preferences prefs;
    private CIIFDatabase db;
    private CIIFView main;

    /** Creates new form CIIFConfigure */
    public CIIFConfigure(CIIFDatabase d, CIIFView w) {
        initComponents();
         // Custom Init
        db = d;
        main = w;
        prefs = Preferences.userNodeForPackage(this.getClass());
        loadPreferences();
        fileChooser = new javax.swing.JFileChooser();
        fileChooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
    }

    public void configureSetLocationFix() { setLocationRelativeTo(CIIFApp.getApplication().getMainFrame()); }

    private void loadPreferences() {
        configureTempFolderInput.setText(prefs.get("TEMPFOLDER", ""));
        configureDBFolderInput.setText(prefs.get("DBDIR", ""));
    }
    
    private void savePreferences() {
        prefs.put("TEMPFOLDER", configureTempFolderInput.getText());
        if (!(configureDBFolderInput.getText().equals(prefs.get("DBDIR", "")))) {
            prefs.put("DBDIR", configureDBFolderInput.getText());
            if (db.checkDB()) {
                db.endApp();
                db = main.reloadDB();
                if (!db.loadDB(this)) {
                    //String msg = "Database Directory is invalid or unset.  Please configure a valid one from the Options -> Settings menu.";
                    //javax.swing.JOptionPane.showMessageDialog(CIIFApp.getApplication().getMainFrame(), msg, "Error: Invalid Database Directory", javax.swing.JOptionPane.ERROR_MESSAGE);
                    //main.openConfigurePane();
                }
            } else {
                System.out.println(db.checkDB());
                if (!db.loadDB(this)) {
                    //String msg = "Database Directory is invalid or unset.  Please configure a valid one from the Options -> Settings menu.";
                    //javax.swing.JOptionPane.showMessageDialog(CIIFApp.getApplication().getMainFrame(), msg, "Error: Invalid Database Directory", javax.swing.JOptionPane.ERROR_MESSAGE);
                    //main.openConfigurePane();
                }
            }
        }
    }
    
    private void enableTree(Container root, boolean enable) {
        for (Component component : root.getComponents()) {
            if (component instanceof Container) {
                enableTree((Container)component, enable);
                component.setEnabled(enable);
            } else {
                component.setEnabled(enable);
            }
        }
    }

   public void disableAll() {
        enableTree(configurePanel, false);
   }

    public void enableAll() {
        enableTree(configurePanel, true);
        configureApplyButton.setText("Apply");
   }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
     @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        configurePanel = new javax.swing.JPanel();
        configureGeneralOptionsPanel = new javax.swing.JPanel();
        configureTempFolderLabel = new javax.swing.JLabel();
        configureTempFolderBrowseButton = new javax.swing.JButton();
        configureTempFolderInput = new javax.swing.JTextField();
        configureDBFolderLabel = new javax.swing.JLabel();
        configureDBFolderInput = new javax.swing.JTextField();
        configureDBFolderBrowseButton = new javax.swing.JButton();
        configureApplyButton = new javax.swing.JButton();
        configureCancelButton = new javax.swing.JButton();
        configureOKButton = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ciif.CIIFApp.class).getContext().getResourceMap(CIIFConfigure.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        configurePanel.setName("configurePanel"); // NOI18N

        configureGeneralOptionsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("configureGeneralOptionsPanel.border.title"))); // NOI18N
        configureGeneralOptionsPanel.setName("configureGeneralOptionsPanel"); // NOI18N

        configureTempFolderLabel.setText(resourceMap.getString("configureTempFolderLabel.text")); // NOI18N
        configureTempFolderLabel.setToolTipText(resourceMap.getString("configureTempFolderLabel.toolTipText")); // NOI18N
        configureTempFolderLabel.setName("configureTempFolderLabel"); // NOI18N

        configureTempFolderBrowseButton.setText(resourceMap.getString("configureTempFolderBrowseButton.text")); // NOI18N
        configureTempFolderBrowseButton.setName("configureTempFolderBrowseButton"); // NOI18N
        configureTempFolderBrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                configureTempFolderBrowseButtonActionPerformed(evt);
            }
        });

        configureTempFolderInput.setText(resourceMap.getString("configureTempFolderInput.text")); // NOI18N
        configureTempFolderInput.setName("configureTempFolderInput"); // NOI18N

        configureDBFolderLabel.setText(resourceMap.getString("configureDBFolderLabel.text")); // NOI18N
        configureDBFolderLabel.setName("configureDBFolderLabel"); // NOI18N

        configureDBFolderInput.setText(resourceMap.getString("configureDBFolderInput.text")); // NOI18N
        configureDBFolderInput.setName("configureDBFolderInput"); // NOI18N
        configureDBFolderInput.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                configureDBFolderInputFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                configureDBFolderInputFocusLost(evt);
            }
        });

        configureDBFolderBrowseButton.setText(resourceMap.getString("configureDBFolderBrowseButton.text")); // NOI18N
        configureDBFolderBrowseButton.setName("configureDBFolderBrowseButton"); // NOI18N
        configureDBFolderBrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                configureDBFolderBrowseButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout configureGeneralOptionsPanelLayout = new javax.swing.GroupLayout(configureGeneralOptionsPanel);
        configureGeneralOptionsPanel.setLayout(configureGeneralOptionsPanelLayout);
        configureGeneralOptionsPanelLayout.setHorizontalGroup(
            configureGeneralOptionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(configureGeneralOptionsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(configureGeneralOptionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(configureDBFolderLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(configureTempFolderLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(configureGeneralOptionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(configureDBFolderInput, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
                    .addComponent(configureTempFolderInput, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(configureGeneralOptionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(configureTempFolderBrowseButton)
                    .addComponent(configureDBFolderBrowseButton))
                .addContainerGap())
        );
        configureGeneralOptionsPanelLayout.setVerticalGroup(
            configureGeneralOptionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(configureGeneralOptionsPanelLayout.createSequentialGroup()
                .addGroup(configureGeneralOptionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(configureTempFolderLabel)
                    .addComponent(configureTempFolderBrowseButton)
                    .addComponent(configureTempFolderInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(configureGeneralOptionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(configureDBFolderLabel)
                    .addComponent(configureDBFolderInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(configureDBFolderBrowseButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        configureApplyButton.setText(resourceMap.getString("configureApplyButton.text")); // NOI18N
        configureApplyButton.setName("configureApplyButton"); // NOI18N
        configureApplyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                configureApplyButtonActionPerformed(evt);
            }
        });

        configureCancelButton.setText(resourceMap.getString("configureCancelButton.text")); // NOI18N
        configureCancelButton.setName("configureCancelButton"); // NOI18N
        configureCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                configureCancelButtonActionPerformed(evt);
            }
        });

        configureOKButton.setText(resourceMap.getString("configureOKButton.text")); // NOI18N
        configureOKButton.setName("configureOKButton"); // NOI18N
        configureOKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                configureOKButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout configurePanelLayout = new javax.swing.GroupLayout(configurePanel);
        configurePanel.setLayout(configurePanelLayout);
        configurePanelLayout.setHorizontalGroup(
            configurePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(configurePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(configurePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(configureGeneralOptionsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, configurePanelLayout.createSequentialGroup()
                        .addComponent(configureOKButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(configureApplyButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(configureCancelButton)))
                .addContainerGap())
        );
        configurePanelLayout.setVerticalGroup(
            configurePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(configurePanelLayout.createSequentialGroup()
                .addComponent(configureGeneralOptionsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 382, Short.MAX_VALUE)
                .addGroup(configurePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(configureCancelButton)
                    .addComponent(configureApplyButton)
                    .addComponent(configureOKButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(configurePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(configurePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void configureTempFolderBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configureTempFolderBrowseButtonActionPerformed
         int returnVal = fileChooser.showOpenDialog(configureGeneralOptionsPanel);
         if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
              File file = fileChooser.getSelectedFile();
              configureTempFolderInput.setText(file.getPath());
         }       
    }//GEN-LAST:event_configureTempFolderBrowseButtonActionPerformed

    private void configureCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configureCancelButtonActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_configureCancelButtonActionPerformed

    private void configureApplyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configureApplyButtonActionPerformed
        savePreferences();        
    }//GEN-LAST:event_configureApplyButtonActionPerformed

    private void configureOKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configureOKButtonActionPerformed
        savePreferences();
        this.setVisible(false);
    }//GEN-LAST:event_configureOKButtonActionPerformed

    private void configureDBFolderBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configureDBFolderBrowseButtonActionPerformed
        if (fileChooser.showOpenDialog(configureGeneralOptionsPanel) == javax.swing.JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            configureDBFolderInput.setText(file.getPath());
        }
    }//GEN-LAST:event_configureDBFolderBrowseButtonActionPerformed

    private void configureDBFolderInputFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_configureDBFolderInputFocusLost
        if (!evt.isTemporary()) {
            if (!(configureDBFolderInput.getText().equals(prefs.get("DBDIR", "")))) {
                String msg = "Are you sure you want to change the DB Path?  Doing so will lose the status of all imported files!";
                int ret = javax.swing.JOptionPane.showConfirmDialog(this, msg, "Change Database Directory?", javax.swing.JOptionPane.YES_NO_OPTION, javax.swing.JOptionPane.QUESTION_MESSAGE, null);
                if (ret == 1) {
                    configureDBFolderInput.setText(prefs.get("DBDIR", ""));
                    configureOKButton.setEnabled(true);
                } else {
                    File dbDir = new File(configureDBFolderInput.getText());
                    if (! dbDir.isDirectory() || ! dbDir.canWrite()) {
                        String msg2 = "Invalid database directory (" + dbDir.getPath() + " doesn't exist or is not writable)";
                        javax.swing.JOptionPane.showMessageDialog(this, msg2, "Error: Invalid Database Directory", javax.swing.JOptionPane.ERROR_MESSAGE);
                        configureDBFolderInput.setText(prefs.get("DBDIR", ""));
                        configureOKButton.setEnabled(true);
                    } else {
                        configureOKButton.setEnabled(false);
                        configureApplyButton.setText("Activate DB Changes");
                    }
                }
            }
        }
    }//GEN-LAST:event_configureDBFolderInputFocusLost

    private void configureDBFolderInputFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_configureDBFolderInputFocusGained
        configureOKButton.setEnabled(false);
    }//GEN-LAST:event_configureDBFolderInputFocusGained

    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CIIFConfigure(db).setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton configureApplyButton;
    private javax.swing.JButton configureCancelButton;
    private javax.swing.JButton configureDBFolderBrowseButton;
    private javax.swing.JTextField configureDBFolderInput;
    private javax.swing.JLabel configureDBFolderLabel;
    private javax.swing.JPanel configureGeneralOptionsPanel;
    private javax.swing.JButton configureOKButton;
    private javax.swing.JPanel configurePanel;
    private javax.swing.JButton configureTempFolderBrowseButton;
    private javax.swing.JTextField configureTempFolderInput;
    private javax.swing.JLabel configureTempFolderLabel;
    // End of variables declaration//GEN-END:variables

}
